<template>
	<view class="detail">

		<v-tabs v-if="tabs.length>0" id="tabs" class="tabs" v-model="current" :tabs="tabs" @change="changeTab" :scroll="false"
		 lineColor="#B8212B" activeColor="#B8212B" :bold="false" lineHeight="8rpx"></v-tabs>

		<jhj-card title="公告速览" v-if="type=='招标公告'" :class="{ margintop: tabs.length>0}">
			<jhj-card-item leftText="项目编号：" :rightText="biddingDetail.project_code"></jhj-card-item>
			<jhj-card-item leftText="项目名称：" :rightText="biddingDetail.project_name"></jhj-card-item>
			<jhj-card-item leftText="招标方式：" :rightText="biddingDetail.bidding_type"></jhj-card-item>
			<jhj-card-item leftText="招标限定：" :rightText="biddingDetail.regist_qualification_set"></jhj-card-item>
			<jhj-card-item leftText="业务类型：" :rightText="biddingDetail.bidding_classification!='整建制'?biddingDetail.tag_ids:'整建制'"></jhj-card-item>
			<jhj-card-item leftText="发布时间：" :rightText="biddingDetail.create_date"></jhj-card-item>
		</jhj-card>

		<jhj-card title="项目概况" v-if="type=='招标公告'">
			<jhj-card-item leftText="项目地点：" :rightText="biddingDetail.project_address"></jhj-card-item>
			<jhj-card-item leftText="项目规模：" :rightText="biddingDetail.project_gczj+'万元，'+biddingDetail.project_jzmj+'平方米'"></jhj-card-item>
			<jhj-card-item leftText="项目工期：" :rightText="biddingDetail.project_begin_date+'至'+biddingDetail.project_end_date"></jhj-card-item>
		</jhj-card>

		<jhj-card title="投标要求" v-if="type=='招标公告'">

			<jhj-card-item leftText="投标要求：" :rightText="biddingDetail.other_matters"></jhj-card-item>
			<!-- <jhj-card-item leftText="资质要求：" rightText="具备钢筋工技能证书"></jhj-card-item>
			<jhj-card-item leftText="业绩要求：" rightText="2019年至今承担过类似项目，并提供相关业绩证明材料"></jhj-card-item>
			<jhj-card-item leftText="最低人数：" rightText="承诺中标后现场在岗人员不得少于50人，并提供相关人员花名册"></jhj-card-item>
			<jhj-card-item leftText="苏安码：" rightText="承诺中标后现场在岗人员均已取得苏安码"></jhj-card-item> -->

		</jhj-card>
		<jhj-card title="投标信息" v-if="type=='招标公告'">
			<jhj-card-item leftText="网上报名截止：" :rightText="biddingDetail.regist_end_date"></jhj-card-item>
			<jhj-card-item leftText="网上投标截止：" :rightText="biddingDetail.tender_end_date"></jhj-card-item>
			<jhj-card-item leftText="现场开标时间：" :rightText="biddingDetail.open_bid_date"></jhj-card-item>
			<jhj-card-item leftText="现场开标地点：" :rightText="biddingDetail.address"></jhj-card-item>

		</jhj-card>
		<jhj-card title="联系方式" v-if="type=='招标公告'">
			<jhj-card-item leftText="招标单位：" :rightText="biddingDetail.company_id_name"></jhj-card-item>
			<jhj-card-item leftText="联系人：" :rightText="biddingDetail.contacts"></jhj-card-item>
			<jhj-card-item leftText="电话：" :rightText="biddingDetail.mobile"></jhj-card-item>

		</jhj-card>

		<view class="detail-card" v-if="type=='招标公告'">
			<view class="card-title">
				招标文件
			</view>
			<view class="card-content">
				<view class="card-content-row" v-for="(item,index) in biddingDetail.attachment">
					<text @click="openDocument(item)">{{item.name}}</text>
				</view>
			</view>
		</view>
		<view class="apply" v-if="type=='招标公告'&&isSignUp" :style="{background:'#ccc'}">
			已报名
		</view>
		<view class="apply" v-if="type=='招标公告'&&isWin" :style="{background:'#ccc'}">
			已中标
		</view>
	
		<view class="apply" v-if="type=='招标公告'&&isRegistrationTime" :style="{background:'#ccc'}">
			报名已结束
		</view>
		
		<view class="apply" v-if="type=='招标公告'&&!isRegistrationTime&&!isWin&&!isSignUp" @click="apply">
			立即报名
		</view>


		<jhj-card title="项目信息" v-if="type=='中标公告'" :class="{ margintop: tabs.length>0}">
			<jhj-card-item leftText="项目编号：" :rightText="biddingDetail.project_code"></jhj-card-item>
			<jhj-card-item leftText="项目名称：" :rightText="biddingDetail.project_name"></jhj-card-item>
		</jhj-card>
		<jhj-card title="开标信息" v-if="type=='中标公告'">
			<jhj-card-item leftText="开标时间：" :rightText="biddingDetail.open_bid_date"></jhj-card-item>
			<jhj-card-item leftText="开标地址：" :rightText="biddingDetail.address"></jhj-card-item>
		</jhj-card>
		<jhj-card title="中标信息" v-if="type=='中标公告'">
			<!-- <jhj-card-item :leftText="unusual2Data.html" ></jhj-card-item> -->
			<rich-text :nodes="biddingDetail.html"></rich-text>
		</jhj-card>
		<jhj-card title="联系方式" v-if="type=='中标公告'">
			<jhj-card-item leftText="联系人：" :rightText="biddingDetail.contacts"></jhj-card-item>
			<jhj-card-item leftText="电话：" :rightText="biddingDetail.mobile"></jhj-card-item>
		</jhj-card>
		<view class="detail-card" v-if="type=='中标公告'">
			<view class="card-title">
				中标文件
			</view>
			<view class="card-content">
				<view class="card-content-row" v-for="(item,index) in biddingDetail.attachment">
					<text @click="openDocument(item)">{{item.name}}</text>
				</view>
			</view>
		</view>

		<jhj-card title="项目信息" v-if="type=='更正公告'" :class="{ margintop: tabs.length>0}">
			<jhj-card-item leftText="项目编号：" :rightText="biddingDetail.project_code"></jhj-card-item>
			<jhj-card-item leftText="项目名称：" :rightText="biddingDetail.project_name"></jhj-card-item>
		</jhj-card>
		<jhj-card title="更正信息" v-if="type=='更正公告'">
			<rich-text :nodes="biddingDetail.html"></rich-text>
			<!-- <jhj-card-item leftText="更正事项：" rightText="招标公告"></jhj-card-item>
			<jhj-card-item leftText="更正内容：" rightText="1、招标公告“4、成交服务费：在领取成交通知书时按预算金额的1.5%向采购代理机构支付服务费。”调整为：“4、成交服务费：在 领取成交通知书时按500元/家向平台支付服务费。”2、开标时间延长至：2021年1月25日"></jhj-card-item> -->
		</jhj-card>
		<jhj-card title="联系方式" v-if="type=='更正公告'">
			<jhj-card-item leftText="联系人：" :rightText="biddingDetail.contacts"></jhj-card-item>
			<jhj-card-item leftText="电话：" :rightText="biddingDetail.mobile"></jhj-card-item>
		</jhj-card>
		<view class="detail-card" v-if="type=='更正公告'">
			<view class="card-title">
				更正文件
			</view>
			<view class="card-content">
				<view class="card-content-row" v-for="(item,index) in biddingDetail.attachment">
					<text @click="openDocument(item)">{{item.name}}</text>
				</view>
			</view>
		</view>

		<uni-popup ref="popup1" type="center" :maskClick="false">
			<view class="authorize">
				<image class="auth-icon" src="@/static/image/authorize.png" mode=""></image>
				<text class="auth-title">授权申请</text>
				<view class="line">

				</view>
				<text class="auth-content">申请获取您的公开信息（昵称、头像）</text>
				<view class="button-group">

					<button class="cancel-btn" @click="cancel">取消</button>
					<button class="auth-btn" open-type="getUserInfo" @getuserinfo="getuserinfo">授权</button>
				</view>

			</view>
		</uni-popup>
	</view>
</template>

<script>
	import JhjCard from '@/components/jhj-card/jhj-card.vue'
	import JhjCardItem from '@/components/jhj-card/jhj-card-item.vue'
	import VTabs from '@/components/v-tabs/v-tabs.vue'
	import {
		getBiddingDetail,
		apply,
		getProcessList,
		getUnusualData,
		getBiddingDetail2
	} from '@/static/js/api/bidding.js';
	import request from '@/static/js/request.js';
	var self;
	export default {
		components: {
			JhjCard,
			JhjCardItem,
			VTabs
		},
		data() {
			return {
				tabs: [],
				tabIndex: 0,
				current: '',
				//招标公告，中标公告，更正公告
				type: '',
				id: '',
				biddingDetail: {},
				unusual2Data: {},
				unusual3Data: {},
				type1: '',
				biddingList: [],
				isSignUp: false,
				isWin: false, //是否已经是中标的
				isRegistrationTime: false, //是否还在报名时间
			}
		},
		onLoad(option) {
			this.id = option.id;
			this.type1 = option.type;
			this.getBiddingDetail();
			self = this;
		},
		methods: {
			getList() {
				let uid = JSON.parse(uni.getStorageSync('userInfo')).uid;
				getProcessList(this.biddingDetail.bidding_id, uid).then(res => {
					if (self.isWin) {
						this.isSignUp = false;
						this.isRegistrationTime = false;

					} else {
						if (res.data.length > 0) {
							this.isSignUp = true;
						}
					}

				})
			},
			changeTab(e) {
				let tab = this.tabs[e];
				if (tab == '变更公告') {
					this.type = '更正公告';
					this.biddingDetail = this.biddingList.filter(f => {
						return f.type == tab;
					})[0];
					this.biddingDetail.tag_ids = this.biddingDetail.tag_ids_name.join(',');

				} else if (tab == '中标') {
					this.type = '中标公告';
					this.biddingDetail = this.biddingList.filter(f => {
						return f.type == tab;
					})[0];
					this.biddingDetail.tag_ids = this.biddingDetail.tag_ids_name.join(',');

				} else {
					this.type = '招标公告';
					this.biddingDetail = this.biddingList.filter(f => {
						return f.type == tab;
					})[0];
					this.biddingDetail.tag_ids = this.biddingDetail.tag_ids_name.join(',');

				}

			},
			getBiddingDetail() {
				getBiddingDetail2(this.id).then(res => {
					// console.log(res);
					if (this.type1 == '变更公告') {
						this.type = '更正公告';
						this.biddingDetail = res.data.filter(f => {
							return f.type == this.type1
						})[0].data[0];
						this.biddingDetail.tag_ids = this.biddingDetail.tag_ids_name.join(',');

					} else if (this.type1 == '中标') {
						this.type = '中标公告';
						this.biddingDetail = res.data.filter(f => {
							return f.type == this.type1
						})[0].data[0];
						this.biddingDetail.tag_ids = this.biddingDetail.tag_ids_name.join(',');
					} else {
						this.type = '招标公告';
						res.data.forEach(item => {
							if (item.data.length > 0) {
								this.tabs.push(item.type);
								this.biddingList.push(item.data[0])
							}
						})
						this.biddingDetail = res.data[0].data[0];
						this.biddingDetail.tag_ids = this.biddingDetail.tag_ids_name.join(',');
						if (res.data[2].data.length > 0) {
							self.isWin = true;
						}
						let date1 = new Date(this.biddingDetail.regist_end_date.replace(/-/g, '/'));
						let nowDate = new Date();
						if (date1.getTime() > nowDate.getTime()) {
							self.isRegistrationTime = false
						} else {
							self.isRegistrationTime = true
						}
					}
					// if (this.biddingDetail.state == '确认中标') {
					// 	this.type = '中标公告';
					// 	this.tabs = ['招标公告详情', '中标公告详情']
					// 	this.getUnusualData();
					// }else{
					// 	if(this.biddingDetail.unusual3_id){
					// 		this.type = '更正公告';
					// 		this.tabs = ['招标公告详情', '更正公告详情']
					// 		this.getUnusual3Data();
					// 	}
					// }
					// this.getList();
				})
			},
			// //获取中标公告信息
			// getUnusualData() {
			// 	getUnusualData(this.biddingDetail.unusual2_id).then(res => {
			// 		this.unusual2Data = res.data[0];
			// 	})
			// },
			// //获取更正公告详情
			// getUnusual3Data() {
			// 	getUnusualData(this.biddingDetail.unusual3_id).then(res => {
			// 		this.unusual3Data = res.data[0];
			// 	})
			// },
			// openDocument(item) {
			// 	if (self.isPicture(item.url)) {
			// 		uni.previewImage({
			// 			urls: [item.url]
			// 		})
			// 	} else {
			// 		//#ifdef MP-WEIXIN
			// 		uni.downloadFile({
			// 			url:  item.url,
			// 			success: function(res) {
			// 				var filePath = res.tempFilePath;
			// 				uni.openDocument({
			// 					filePath: filePath,
			// 					showMenu: true, // 允许出现分享功能
			// 					success: function(res) {
			// 						console.log('打开文档成功');
			// 					}
			// 				});
			// 			}
			// 		});
			// 		//#endif
			// 	}
			// },
			openDocument(item) {
				wx.downloadFile({
					url: item.url,
					success: function(res) {
						// console.log(res);
						const tempFilePath = res.tempFilePath;
						// 保存文件
						wx.saveFile({
							tempFilePath,
							success: function(res) {
								const savedFilePath = res.savedFilePath;
								// 打开文件
								wx.openDocument({
									filePath: savedFilePath,
									success: function(res) {
										console.log('打开文档成功')
									},
								});
							},
							fail: function(err) {
								console.log('保存失败：', err)
							}
						});
					},
					fail: function(err) {
						console.log('下载失败：', err);
					},
				});
			},
			//申请
			// apply() {
			// 	let isVerified = uni.getStorageSync('isVerified')
			// 	let userInfoS = uni.getStorageSync('userInfo')
			// 	// console.log(userInfoS);
			// 	if (!userInfoS) {
			// 		self.$refs.popup1.open();
			// 		return;
			// 	}
			// 	let userInfo = JSON.parse(userInfoS);
			// 	if (userInfo.account_type == '工人') {
			// 		uni.showToast({
			// 			icon: 'none',
			// 			title: '请先注册成为班组或者企业'
			// 		})
			// 		return;
			// 	}
				
			// 	 const accountType =userInfo.account_type;
			// 	      if (
			// 	        (this.biddingDetail.regist_qualification_set === "企业" &&
			// 	          accountType === "班组长") ||
			// 	        (this.biddingDetail.regist_qualification_set === "班组" &&
			// 	          accountType === "劳务企业")
			// 	      ) {
			// 			  uni.showToast({
			// 			  	icon: 'none',
			// 			  	title: '您不符合报名资质要求，感谢您的参与'
			// 			  })
				       
			// 	        return;
			// 	      }
			// 	if (isVerified) {
			// 		let date1 = new Date(this.biddingDetail.regist_end_date.replace(/-/g, '/'));
			// 		let nowDate = new Date();
			// 		if (date1.getTime() > nowDate.getTime()) {
			// 			let uid = JSON.parse(uni.getStorageSync('userInfo')).uid;
			// 			getProcessList(this.biddingDetail.bidding_id, uid).then(res => {
			// 				if (res.data.length > 0) {
			// 					uni.showToast({
			// 						icon: 'none',
			// 						title: '已报名'
			// 					})
			// 				} else {
			// 					let data = {
			// 						["form-field-many2one-order_id"]: this.biddingDetail.bidding_id,
			// 						["form-field-many2one-user_id"]: uid,
			// 					}
			// 					apply(data).then(res => {
			// 						uni.showToast({
			// 							title: '报名成功'
			// 						})
			// 					})
			// 				}
			// 			})
			// 		} else {
			// 			uni.showToast({
			// 				icon: 'none',
			// 				title: '报名时间已过'
			// 			})
			// 		}
			// 	} else {
			// 		uni.showModal({
			// 			title: '提示',
			// 			content: '请先完成实名认证',
			// 			success(r) {
			// 				if (r.confirm) {
			// 					uni.navigateTo({
			// 						url: '/pages/main/mine/MineRegisterIdentity'
			// 					})
			// 				}

			// 			}
			// 		})
			// 	}
			// },
			apply(){
				uni.showToast({
					icon: 'none',
					title: '请到pc端参与报名',
					duration:2000
				})
			},
			isPicture(str) {
				//判断是否是图片 - strFilter必须是小写列举

				// return [
				// 	'png', 'jpg', 'jpeg', 'bmp', 'gif', 'webp', 'svg',
				// ].indexOf(str.toLowerCase()) !== -1;

				var strFilter = ".jpeg|.gif|.jpg|.png|.bmp|.pic|.svg|"
				if (str.indexOf(".") > -1) {
					var p = str.lastIndexOf(".");
					var strPostfix = str.substring(p, str.length) + '|';
					strPostfix = strPostfix.toLowerCase();
					if (strFilter.indexOf(strPostfix) > -1) {
						return true;
					}
				}
				return false;
			},
			getuserinfo(e) {
				if (e.detail.rawData) {
					uni.showLoading({
						title: '正在登录'
					})
					uni.login({
						success: function(loginRes) {
							var code = loginRes.code;
							uni.getUserInfo({
								success: (res) => {
									// console.log(res);
									let data = {
										code,
										encryptedData: res.encryptedData,
										iv: res.iv,
										vmp: 'vmp'
									};
									request.post('/api/v1/signup/0', data).then(resp => {
										uni.setStorageSync('userInfo', JSON.stringify(resp.data))
										uni.hideLoading();
										self.$refs.popup1.close();

									}).catch(e => {
										uni.showToast({
											icon: 'none',
											title: e.message
										})
									})
								},
								fail: () => {
									console.log("未授权");
								}
							})
						}
					});
				}

			},
			cancel() {
				this.$refs.popup1.close()
			}
		}
	}
</script>

<style lang="less" scoped>
	.tabs {
		position: fixed;
		/* #ifdef H5 */
		top: 80rpx;
		/* #endif */
		/* #ifdef MP-WEIXIN */
		top: 0rpx;
		/* #endif */
		left: 0;
		right: 0;
	}

	.margintop {
		margin-top: 90rpx;
		display: block;
	}

	.detail {
		background: #F4F5F9;
		padding-bottom: 40rpx;
		overflow: hidden;
	}

	.detail-card {
		padding: 30rpx;
		background: #fff;
		margin-bottom: 20rpx;

		.card-title {
			font-size: 36rpx;
			font-weight: 500;
			color: #333333;
			margin-bottom: 24rpx;
		}

		.card-content {
			.card-content-row {
				display: flex;
				font-size: 28rpx;
				font-weight: 500;
				color: #666666;
				line-height: 40rpx;
				margin-bottom: 6rpx;

				.row-left {}

				.row-right {
					flex: 1;
				}

				text {
					font-size: 28rpx;
					font-weight: 500;
					color: #B8212B;
				}
			}
		}
	}

	.apply {
		width: 680rpx;
		height: 80rpx;
		background: #B8212B;
		border-radius: 8rpx;
		line-height: 80rpx;
		text-align: center;
		margin: 40rpx auto;
		font-size: 32rpx;
		font-weight: 500;
		color: #FFFFFF;
	}

	.authorize {

		background: #FFFFFF;
		border-radius: 8rpx;
		padding: 40rpx 26rpx 44rpx;
		display: flex;
		flex-direction: column;
		align-items: center;

		.auth-icon {
			width: 66rpx;
			height: 76rpx;
			margin-bottom: 12rpx;
		}

		.auth-title {
			font-size: 36rpx;
			font-weight: 500;
			color: #333333;
		}

		.auth-content {
			font-size: 30rpx;
			font-weight: 500;
			color: #333333;
		}

		.button-group {
			display: flex;
			align-items: center;
			margin-top: 38rpx;
			width: 400rpx;
		}

		.line {

			height: 2rpx;
			background: #EEEEEE;
			width: 100%;
			margin: 22rpx 0;
		}

		.auth-btn {

			flex: 1;
			// width: 300rpx;
			height: 68rpx;
			background: #B8212B;
			border-radius: 2rpx;
			border: 2rpx solid #B8212B;
			line-height: 68rpx;
			font-size: 28rpx;

			font-weight: 400;
			color: #FFFFFF;
		}

		.cancel-btn {

			flex: 1;
			// width: 300rpx;
			height: 68rpx;
			color: #B8212B;
			border-radius: 2rpx;
			border: 2rpx solid #B8212B;
			line-height: 68rpx;
			font-size: 28rpx;
			margin-right: 20rpx;

			font-weight: 400;

		}
	}
</style>
